//
//  ViewController.swift
//  TestWindowSheet
//
//  Created by 杨岑 on 2018/9/7.
//  Copyright © 2018年 杨岑. All rights reserved.
//

import Cocoa

class MainVC: NSViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        print("NSApplication.ModalResponse codes:")
        print("OK = \(NSApplication.ModalResponse.OK)")
        print("Cancel = \(NSApplication.ModalResponse.cancel)")
        print("continue = \(NSApplication.ModalResponse.continue)")
        print("stop = \(NSApplication.ModalResponse.stop)")
        print("abort = \(NSApplication.ModalResponse.abort)")
        print("1st = \(NSApplication.ModalResponse.alertFirstButtonReturn)")
        print("2nd = \(NSApplication.ModalResponse.alertSecondButtonReturn)")
        print("3rd = \(NSApplication.ModalResponse.alertThirdButtonReturn)")
    }

    @IBAction func testAlertSheet(_ sender: Any) {
        let alert = NSAlert()
        alert.alertStyle = .warning
        alert.icon = NSImage(named: NSImage.infoName)
        alert.messageText = "This is a confirmation alert sample."
        alert.informativeText = "Informative text."
        alert.addButton(withTitle: "OK")
        alert.addButton(withTitle: "Cancel")
        alert.addButton(withTitle: "Another Button")
        alert.showsHelp = true
        alert.showsSuppressionButton = true
        
        if let button = sender as? NSButton {
            if button.tag == 0 {
                alert.beginSheetModal(for: self.view.window!) { (responseCode) in
                    print(Thread.current)
                    print("Alert sheet completed with response code \(responseCode)")
                }
            } else {
                let responseCode = alert.runModal()
                print("Alert runModal completed with response code \(responseCode)")
            }
        }
    }
    
    @IBAction func testPresentAsSheet(_ sender: Any) {
        let panelVC = storyboard?.instantiateController(withIdentifier: "panel") as! PanelVC
        panelVC.completionHandler = { (sender) in
            print("Panel View completed: \(sender)")
            print(Thread.current)
            // Sheet completion is in main thread, but it is better to
            // schedule UI work using main operation queue.
            OperationQueue.main.addOperation {
                print("Updating UI in main Q...")
            }
        }
        if let button = sender as? NSButton {
            button.tag == 0 ?
                self.presentAsSheet(panelVC) :
                self.presentAsModalWindow(panelVC)
        }
    }
}

